//
//  ShaidanTextImageCell.m
//  danpin
//
//  Created by chuangjia on 22/9/15.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "ShaidanTextImageCell.h"
#import "viewconfig.h"
#import "UIPlaceholderTextView.h"
#import "ShaidanModel.h"
@interface ShaidanTextImageCell()<UITextViewDelegate>
{
    
}

@property(nonatomic,strong)UIImageView *imagesView;
@property(nonatomic,strong)UIPlaceholderTextView *mTextView;
@property(nonatomic,strong)UIButton *imagebtn;
@end

@implementation ShaidanTextImageCell

-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        UITapGestureRecognizer* singleOneView= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapForSunDetail)];
        _imagesView =[[UIImageView alloc] initWithFrame:CGRectMake((DEVICE_WIDTH-widthAuto(280))/2, 0, widthAuto(280),hightAuto(150))];
        _imagesView.backgroundColor =UIColorFromRGB(0xf4f4f4);
        _imagesView.image =[UIImage imageNamed:@"cjsd_image.png"];
        _imagesView.userInteractionEnabled=YES;
        [_imagesView addGestureRecognizer:singleOneView];
        [self.contentView addSubview:_imagesView];
        singleOneView=nil;
        
        _imagebtn =[UIButton buttonWithType:UIButtonTypeCustom];
        [_imagebtn setTitleColor:UIColorFromRGB(0xbfbfbf) forState:UIControlStateNormal];
        [_imagebtn setImage:[UIImage imageNamed:@"insert_img.png"] forState:UIControlStateNormal];
        [_imagebtn setTitle:@"  图片" forState:UIControlStateNormal];
        [_imagebtn addTarget:self action:@selector(addIndexImage) forControlEvents:UIControlEventTouchUpInside];
        [_imagesView addSubview:_imagebtn];
      
        
        _mTextView =[[UIPlaceholderTextView alloc]initWithFrame:CGRectZero];
        _mTextView.backgroundColor =[UIColor clearColor];
        _mTextView.delegate = self;
        _mTextView.placeholderColor =[UIColor darkGrayColor];
        _mTextView.textColor = [UIColor darkGrayColor];
        _mTextView.placeholder = @"文字描述...";
        _mTextView.font =FONT(15);
        _mTextView.returnKeyType = UIReturnKeyDone;
        [self.contentView addSubview:_mTextView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChangedWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}
-(void)addIndexImage
{
    
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.actionBlock =nil;
    self.data=nil;
    self.imagesView = nil;
    self.key =nil;
    self.other=nil;
    self.indexPath=nil;
    
    [self.mTextView resignFirstResponder];
    self.mTextView.delegate=nil;
    [self.mTextView removeobserver];
    [self.mTextView removeFromSuperview];
    self.mTextView=nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key =key;
    self.data =model;
    self.indexPath =indexPath;
    if (model==nil) {
        return;
    }
    ShaidanModel *dict =(ShaidanModel *)model;
    CGFloat H =hightAuto(150);
    if (dict.imageview!=nil) {
        CGFloat w = dict.width;
        CGFloat h =dict.height;
        if (w>0 && h>0) {
            CGFloat fullW =DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4;
            CGFloat ratio = fullW / w;
            H = ratio * h;
        }else{
            H=hightAuto(150);
        }
        
    }
    self.imagesView.frame =CGRectMake(PHOTO_FRAME_WIDTH*2, 0, DEVICE_WIDTH -PHOTO_FRAME_WIDTH*4,  H);
    if (dict.ishavaimage) {
        self.imagesView.image =dict.imageview;
    }else{
        self.imagebtn.frame=CGRectMake((self.imagesView.frame.size.width-150)/2, (self.imagesView.frame.size.height-30)/2, 150, 30);
    }
    
    self.mTextView.frame =CGRectMake(PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH+H, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*4);
    
    NSString *txtstr =[[NSString stringWithFormat:@"%@",dict.title] replaceNullString];
    if (txtstr.length==0 || [txtstr isEqualToString:@"(null)"])
    {
        txtstr =@"";
    }
    if (txtstr && txtstr.length>0){
        self.mTextView.textColor = [UIColor darkGrayColor];
        self.mTextView.text =txtstr;
        float height = [[NSString stringWithFormat:@"%@\n ",txtstr] boundingRectWithSize:CGSizeMake(DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:FONT(16),NSFontAttributeName, nil] context:nil].size.height;
        
        self.mTextView.frame =CGRectMake(PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH+H, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, height);
    }
    
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{

    ShaidanModel *dict =(ShaidanModel *)model;
    
    CGFloat H =hightAuto(150);
    if (dict.imageview!=nil) {
        CGFloat w = dict.width;
        CGFloat h =dict.height;
        if (w>0 && h>0) {
            CGFloat fullW =DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4;
            CGFloat ratio = fullW / w;
            H = ratio * h;
        }else{
            H=hightAuto(150);
        }
    }
    NSString *content =[[NSString stringWithFormat:@"%@",dict.title] replaceNullString];
    float height = [[NSString stringWithFormat:@"%@\n ",content] boundingRectWithSize:CGSizeMake(DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:FONT(16),NSFontAttributeName, nil] context:nil].size.height;
    H =  H + height;
    return H+PHOTO_FRAME_WIDTH*2;
    
}
-(void)handleSingleTapForSunDetail
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_shaidan,self,self.data,self.key,self.indexPath);
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.mTextView resignFirstResponder];
    return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_shaidanPosition,self,self.data,self.mTextView.text,self.indexPath);
    }
    return YES;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

-(void)textDidChangedWillHide:(NSNotification*)notification
{
    ShaidanModel *smodel =(ShaidanModel *)self.data;
    smodel.title=self.mTextView.text;
    self.data = smodel;
    
    if (self.actionBlock) {
        self.actionBlock(BEventType_shaidanTxt,self,self.data,self.mTextView.text,self.indexPath);
    }
}

@end
